package com.bolt.support.captcha;

import com.bolt.common.utils.CacheUtil;
import com.bolt.support.cache.redis.RedisManager;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

public class RedisCaptchaCacheService implements CaptchaCacheService {

    private final RedisManager redisManager;

    public RedisCaptchaCacheService(RedisManager redisManager) {
        this.redisManager = redisManager;
    }

    @Override
    public void set(String key, String value, long expiresInSeconds) {
        redisManager.cache(key,value,expiresInSeconds, TimeUnit.SECONDS);
    }

    @Override
    public boolean exists(String key) {
        return !Objects.isNull(get(key));
    }

    @Override
    public void delete(String key) {
        redisManager.del(key);
    }

    @Override
    public String get(String key) {
        return redisManager.getString(key);
    }

    @Override
    public Long increment(String key, long val) {
        Long ret = Long.valueOf(get(key))+val;
        CacheUtil.set(key,ret+"",60);
        return ret;
    }
}